package newspapers;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

import javax.jms.JMSException;

import model.Newspaper;

/**
 * Example of newspaper: The Times.
 */
public class TheTimes extends Newspaper {

    /**
     * Constructor.
     *
     * @param name  name of the newspaper
     * @param username  username to use to connect to the broker
     * @param password  password to use to connect to the broker
     * @param topicPrefix  prefix in the topics of the newspaper
     * @param topics  list of topics to publish to
     */
    public TheTimes(final String name, final String username,
                    final String password, final String topicPrefix,
                    final Set<String> topics) {
        super(name, username, password, topicPrefix, topics);
    }

    /**
     * Creates The Times and publishes a few messages.
     *
     * @param args  array of arguments from the command line (unused)
     */
    public static void main(String[] args) {

        //create The Times
        TheTimes theTimes = new TheTimes("The Times", "guest", "guest",
                                         "times", new HashSet<String>());
        theTimes.addTopic("times.sport");
        theTimes.addTopic("times.music");
        theTimes.addTopic("times.arts");

        try {

	        //publish some messages
	        HashMap<String, String> properties1 = new HashMap<String, String>();
	        properties1.put("author", "Mary Jones");
	        properties1.put("date", "November 09");
	        properties1.put("sport", "football");
	        properties1.put("header", "news1 from times-sport");
	        theTimes.publishMessage("times.sport", "Message about sports from "
	                + theTimes.getName() + " with properties. loooooooooooo ooooo "
	                + "oooooooooooooo oooooooo ooooooooooooooo oooooooo oooooooooo"
	                + "o oooooo ooooooooo ooooooooo oooooooo oooooooooo ooooooo oo"
	                + "ooooo oooooooooo ooooooooo oooooooo ooooooooo ooooooo ooooo"
	                + "oooo ooooooooo oooooooooo oooooooo oooooooo oooooooo oooooo"
	                + "oooo oooooooooo ooooooooooooooooooo ooooooooooooooooooooooo"
	                + "ooooooooooooooo ooooooooooooooooooooong message.",
	                properties1);
	        HashMap<String, String> properties3 = new HashMap<String, String>();
	        properties3.put("header", "news1 from times-music");
	        theTimes.publishMessage("times.music", "Message about music from "
	                + theTimes.getName() + " without properties", properties3);

//	        //wait to turn broker off
//	        for (int i = 0; i < 1000000; i++) {
//	            for (int j = 0; j < 1000000; j++) {
//	            }
//	        }

	        HashMap<String, String> properties4 = new HashMap<String, String>();
	        properties4.put("header", "news2 from times-sport");
	        theTimes.publishMessage("times.sport", "Another message about sports from "
	                + theTimes.getName() + " without properties", properties4);
	        HashMap<String, String> properties2 = new HashMap<String, String>();
	        properties2.put("author", "Harry Williams");
	        properties2.put("date", "November 09");
	        properties2.put("arts", "photography");
	        properties2.put("header", "news1 from times-arts");
	        theTimes.publishMessage("times.arts", "Message about arts from "
	                + theTimes.getName() + " with properties", properties2);

            //close connections
            theTimes.closeConnections();

        } catch (JMSException e) {
            //connections already closed
        }
    }
}
